<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">渠道名称：</label>
                            <div class="col-sm-8">
                                <select id="channelid" onchange="getDistrict()" name="channelid"
                                        class="form-control" type="text">
                                    <option value="">---请选择---</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">sdk名称：</label>
                            <div class="col-sm-8">
                                <select id="sdkid" name="sdkid"
                                        class="form-control" type="text">
                                    <!-- <option value="">-&#45;&#45;请选择-&#45;&#45;</option>-->
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">变现方名称：</label>
                            <div class="col-sm-8">
                                <input maxlength="50" id="thirdname" name="thirdname" class="form-control" type="text">
                            </div>
                        </div>
                        <!-- <div class="form-group">
                             <label class="col-sm-3 control-label">第三方sdkID：</label>
                             <div class="col-sm-8">
                                 <input id="thirdsdkid" name="thirdsdkid" class="form-control" type="text">
                             </div>
                         </div>-->
                        <!--<div class="form-group">
                            <label class="col-sm-3 control-label">第三方sdk名称：</label>
                            <div class="col-sm-8">
                                <input id="thirdsdkname" name="thirdsdkname" class="form-control" type="text">
                            </div>
                        </div>-->
                        <div class="form-group">
                            <label class="col-sm-3 control-label">变现方sdk版本号：</label>
                            <div class="col-sm-8">
                                <input onkeyup="value=value.replace(/[^\d^\.]+/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')" maxlength="50" id="thirdsdkver" name="thirdsdkver" class="form-control"
                                       type="text">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">是否运营：</label>
                            <div class="col-sm-8">
                                <select id="isoperate" name="isoperate" class="form-control">
                                    <option value="1">运营</option>
                                    <option value="0">不运营</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" id="dddddd">
                            <label class="col-sm-3 control-label">sdk包名：</label>
                            <div class="col-sm-8">
                                <input maxlength="50" id="sdkpackname" name="sdkpackname" class="form-control"
                                       type="text">
                            </div>
                        </div>
                        <div class="form-group" id="bbbbbb">
                            <label class="col-sm-3 control-label">sdk方法名：</label>
                            <div class="col-sm-8">
                                <input maxlength="50" id="sdkmethodname" name="sdkmethodname" class="form-control"
                                       type="text">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">是否控量：</label>
                            <div class="col-sm-8">
                                <select onchange="getControlquantity()" id="iscontrolquantity" name="iscontrolquantity"
                                        class="form-control">
                                    <option value="1">控量</option>
                                    <option value="0">不控量</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" id="aaaaa">
                            <label class="col-sm-3 control-label">控制数量：</label>
                            <div class="col-sm-8">
                                <input maxlength="10" onkeyup="this.value=this.value.replace(/\D/g,'')"
                                       onafterpaste="this.value=this.value.replace(/\D/g,'')" id="controlquantity"
                                       name="controlquantity" class="form-control" type="text">
                            </div>
                        </div>
                        <!-- <div class="form-group" id="cccccc">
                             <label class="col-sm-3 control-label">下载地址：</label>
                             <div class="col-sm-8">
                                 <input maxlength="100" id="sdkurl" name="sdkurl" class="form-control" type="text">
                             </div>
                         </div>-->
                        <div class="form-group" id="cccccc">
                            <form enctype="multipart/form-data" method="post"
                                  id="updform">
                                <div class="form-group">
                                    <label class="col-md-3 control-label">上传文件</label>
                                    <div class="col-md-9">
                                        <input type="file" id="file" name="file" style="display: inline-block;">
                                        <a id="apkUrl" style="margin-right: 50px;" href="javascript:void(0);"></a>
                                        <button type="button" class="btn btn-primary " id="upload">
                                            上传
                                        </button>
                                        <p class="help-block">请上传相关渠道包.</p>
                                    </div>

                                </div>
                            </form>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">描述：</label>
                            <div class="col-sm-8">
                                <input maxlength="150" id="sdkdes" name="sdkdes" class="form-control" type="text">
                            </div>
                        </div>
                        <input id="sdktype" name="sdktype" th:value="${sdktype}" type="hidden">
                        <input type="hidden" name="sdkurl" id="fileUrl"/>
                        <input type="hidden" name="thirdsdkname" id="fileName"/>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script type="text/javascript" src="/js/appjs/market/thirdSdk/add.js">
</script>
<!--<script th:src="@{//cdn.bootcss.com/jquery.form/4.2.2/jquery.form.js}"></script>-->
<script type="text/javascript" th:inline="javascript">
    $(function () {
        $("#upload").click(function () {
            var oFile = $("#file").val();
            if (oFile == null || "" == oFile) {
                alert("没有文件！请选择或退出");
                return;
            }
            var file = document.querySelector('[type=file]');
            // 通过FormData将文件转成二进制数据
            var formData = new FormData();
            // 将文件转二进制
            formData.append('file', file.files[0]);
            // var bbb = $("#updform:first").serialize()
            // var aaa = new FormData($("#file")[0]);
            // var form = new FormData(document.getElementById("updform"));
            $.ajax({
                type: "POST",
                url: "/market/thirdSdk/uploadFile",
                data: formData,// 你的formid
                async: false,
                processData: false,
                contentType: false,
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (result) {
                    if (result != null) {
                        var name = result.split("#")[0];
                        var url = result.split("#")[1];
                        $("#apkUrl").text(name);
                        $("#apkUrl").attr("onclick", "").click(function () {
                            downFileUrl(url);
                        });
                        $("#fileUrl").val(url);
                        $("#fileName").val(name);
                        layer.msg("上传成功");
                        $("#file").val('');
                    } else {
                        layer.msg("上传失败，请联系管理员！");
                    }
                    /* /!*  if (data.code == 0) {
                           parent.layer.msg("操作成功");
                           parent.reLoad();
                           var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                           parent.layer.close(index);

                       } else {
                           parent.layer.alert(data.msg)
                       }*!/*/

                }
            });
            /*     $("#updform").ajaxSubmit(function (result) {
                     debugger;
                     if (result != null) {
                         var name = result.split("#")[0];
                         var url = result.split("#")[1];
                         $("#apkUrl").text(name);
                         $("#apkUrl").attr("onclick", "").click(function () {
                             downFileUrl(url);
                         });
                         $("#fileUrl").val(url);
                         $("#fileName").val(name);
                         tale.alertOk("上传成功！");
                         $("#file").val('');
                     } else {
                         tale.alertWarn("上传失败，请联系管理员！");
                     }
                 });*/
        });
        $.ajax({
            type: 'get',
            url: '/market/sdk/listAllChannel',
            dataType: 'json',
            success: function (datas) {//返回list数据并循环获取
                var select = $("#channelid");
                /*select.append("<option value='" + "" + "'>"
                    + "---请选择---" + "</option>");*/
                for (var i = 0; i < datas.length; i++) {
                    select.append("<option value='" + datas[i].channelId + "'>"
                        + datas[i].channelName + "</option>");
                }
            }
        });
        var sdktype = $("#sdktype").val()
        if (sdktype == 0) {
            $("#dddddd").hide();
            $("#bbbbbb").hide();
            $("#cccccc").hide();
        }
        /*  $("#sdkid").append("<option value='' selected='selected'>" + "---请选择---" + "</option>");*/

    })

    function downFileUrl(fileUrl) {
        window.location.href = "/market/thirdSdk/downFile?fileUrl=" + fileUrl;
    }

    function getDistrict() {
        $("#sdkid").empty();    //清空二级下拉框，否则会叠加
        // $("#sdkid").append("<option value='' selected='selected'>" + "---请选择---" + "</option>");
        /* var id = obj.id;
         var options = $("#" + id + " option:selected"); //获取选中的项
         alert(options.text()); //拿到选中项的值
         var countrycode = options.val();*/
        var channelid = $("#channelid").val();
        $.ajax({
            type: "post",
            url: '/market/sdk/SdkListByChannelid',
            dataType: 'json',
            data: {channelid: channelid},
            success: function (data) {
                var select = $("#sdkid");
                for (var i = 0; i < data.length; i++) {
                    select.append("<option value='" + data[i].sdkId + "'>"
                        + data[i].sdkName + "</option>");
                }
            }
        });
    }

    function getControlquantity() {
        var icon = "<i class='fa fa-times-circle'></i> ";

        var channelid = $("#iscontrolquantity").val();
        if (channelid == 0) {
            $("#controlquantity").val('');
            $("#signupForm").validate({
                rules: {
                    controlquantity: {
                        required: false
                    }
                }
                ,
                messages: {
                    controlquantity: {
                        required: icon + "请输入控制数量"
                    }

                }
            })

            $("#aaaaa").hide();
        } else {
            $("#aaaaa").show();
            $("#signupForm").validate({
                rules: {
                    controlquantity: {
                        required: true
                    }
                }
                ,
                messages: {
                    controlquantity: {
                        required: icon + "请输入控制数量"
                    }

                }
            })
        }
    }

</script>
</body>
</html>
